//
//  ViewController.m
//  ybUIView
//
//  Created by mac on 15-11-30.
//  Copyright (c) 2015年 1. All rights reserved.
//

#import "ViewController.h"
#import "BarViewController.h"

@interface ViewController ()<UITableViewDelegate,inDelegate>
{
    NSMutableArray *names;
    NSMutableArray *tels;
    NSIndexPath *index;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.title = @"电话簿";
    names = @[@"张三",@"李四",@"王五"].mutableCopy;
    tels = @[@"13612345678",
            @"13311145678",
            @"14713612345"
            ].mutableCopy;
    //self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(onEdit:)];
    self.tableView.allowsSelectionDuringEditing = YES;
//    [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(onEdit)];
}

- (void)onEdit:(UIBarButtonItem *)sender
{
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    sender.title = self.tableView.editing ? @"完成" : @"编辑";
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableView.editing ? names.count + 1 : names.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    if (indexPath.row < names.count) {
        cell.textLabel.text = names[indexPath.row];
        cell.detailTextLabel.text = tels[indexPath.row];
    }
    return cell;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath.row < names.count ? UITableViewCellEditingStyleDelete :UITableViewCellEditingStyleInsert;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    BarViewController *bar = [[BarViewController alloc]initWithStyle:UITableViewStyleGrouped];
    if (indexPath.row < names.count) {
        bar.name = names[indexPath.row];
        bar.tel = tels[indexPath.row];
    }else
    {
        [names addObject:@""];
        [tels addObject:@""];
    }
    index = indexPath;
    bar.delegate = self;
    [self.navigationController pushViewController:bar animated:YES];
    if (tableView.editing) {
        [tableView setEditing:NO animated:YES];
    }
    
}

- (void)editFinishWithName:(NSString *)name tel:(NSString *)tel
{
    NSIndexPath *indexpath = index;
    
    names[indexpath.row] = name;
    tels[indexpath.row] = tel;
    [self.tableView reloadData];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        
        }else
        {
            [names removeObjectAtIndex:indexPath.row];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
        }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
